<template>
  <div>
    <el-row class="gl_mb_15">
      <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="clickAdd('add')">新增级段位</el-button>
    </el-row>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="序号" type="index" :index="table_index" />
      <el-table-column label="级段位名称" prop="name" :min-width="120" />
      <el-table-column label="类型" prop="name" :min-width="100">
        <template slot-scope="{row}">
          {{ row.levelDuanType ? row.levelDuanType.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="具体级别" :min-width="100">
        <template slot-scope="{row}">
          <span v-if="row.levelDuanTypeCode === 'Level'">{{ row.beltLevelType ? row.beltLevelType.name : '' }}</span>
          <span v-if="row.levelDuanTypeCode === 'Duan'">{{ row.beltDuanType ? row.beltDuanType.name : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名费用" min-width="100">
        <template slot-scope="{row}">
          {{ (row.fee/100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="140">
        <template slot-scope="{row}">
          <span v-if="row.creationTime">
            {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="creatorName" min-width="80" />
      <el-table-column label="状态">
        <template slot-scope="{row}">
          <span>{{ row.isEnabled ? '已启用' : '已停用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="{ row }">
          <el-button v-if="row.isEnabled" size="mini" type="text" @click="clickDisable(row.id)">禁用</el-button>
          <el-button v-else size="mini" type="text" @click="clickEnable(row.id)">启用</el-button>
          <el-button size="mini" type="text" @click="clickAdd('edit', row)">编辑</el-button>
          <el-button size="mini" type="text" @click="clickDelete(row.id)"><span class="gl_color_error">删除</span></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="qo.MaxResultCount"
        :current-page="qo.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
    <addDan ref="addDan" />
  </div>
</template>

<script>
import addDan from './addDan.vue';
import { mapState, mapActions } from 'vuex';
import {
  Api_GET_ClubDuanQO // 查询级段位列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubDuan, // 查询级段位列表
  api_POST_ClubDuanIdDisable, // 禁用级段位
  api_POST_ClubDuanIdEnable, // 启用级段位
  api_DELETE_ClubDuanId // 删除级段位
} from '@/extend/api/hallswaggerApi/app';
export default {
  components: { addDan },
  data () {
    return {
      qo: new Api_GET_ClubDuanQO(),
      tableData: [],
      loading: false,
      total: 0,
      chooseTime: []
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      user: state => state.user.currentOU
    })
  },
  created() {
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict' }),
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      this.qo.IsEnabled = null;
      this.qo.Ids = null;
      this.qo.IncludeBeltDuanType = true;
      this.qo.IncludeBeltLevelType = true;
      this.qo.IncludeLevelDuanType = true;
      this.qo.Sorting = 'creationTime DESC';
      this.loading = true;
      api_GET_ClubDuan(this.qo).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 启用
    clickEnable(id) {
      this.loading = true;
      api_POST_ClubDuanIdEnable(id).then(res => {
        this.$message({ type: 'success', message: '启用成功！' });
        this.getPageList();
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 禁用
    clickDisable(id) {
      this.loading = true;
      api_POST_ClubDuanIdDisable(id).then(res => {
        this.$message({ type: 'success', message: '禁用成功！' });
        this.getPageList();
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 删除
    clickDelete(id) {
      this.$confirm(`您确定要删除该级段位吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        api_DELETE_ClubDuanId(id).then(() => {
          this.getPageList();
          this.$message({ type: 'success', message: '删除成功！' });
        }, () => {
          this.loading = false;
          this.$message({ type: 'error', message: '删除失败！' + error.error.message });
        });
      }).catch(() => {});
    },
    clickAdd(type, row) {
      this.$refs.addDan.type = type;
      this.$refs.addDan.dialogVisible = true;
      if (type === 'edit') {
        this.$refs.addDan.qo = {
          name: row.name,
          fee: row.fee / 100,
          isEnabled: row.isEnabled,
          levelDuanTypeCode: row.levelDuanTypeCode,
          beltLevelTypeCode: row.beltLevelTypeCode,
          beltDuanTypeCode: row.beltDuanTypeCode
        };
        this.$refs.addDan.id = row.id;
      }
    },
    handleTimeChange(time) {
      this.qo.StartTime = time ? time[0] : null;
      this.qo.EndTime = time ? time[1] : null;
      this.getPageList();
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    },
    // 序号递增
    table_index(index) {
      return (this.qo.Page - 1) * this.qo.MaxResultCount + index + 1;
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
